package com.cheer.ba.presenter.impl;

import android.content.Context;

import com.cheer.ba.R;
import com.cheer.ba.cache.preference.Session;
import com.cheer.ba.constant.CodeStatus;
import com.cheer.ba.constant.ConstantTag;
import com.cheer.ba.constant.Constants;
import com.cheer.ba.constant.ReturnCode;
import com.cheer.ba.http.BaseCallback;
import com.cheer.ba.http.CallManager;
import com.cheer.ba.http.HandleRetCode;
import com.cheer.ba.http.Http;
import com.cheer.ba.http.request.APIService;
import com.cheer.ba.model.ArticleModel;
import com.cheer.ba.model.CalculateMoneyModel;
import com.cheer.ba.model.MyTradeRecordModel;
import com.cheer.ba.model.OilDetailsModel;
import com.cheer.ba.model.OilGoodsInfoModel;
import com.cheer.ba.model.QyeAuthModel;
import com.cheer.ba.model.ResponseData;
import com.cheer.ba.presenter.GeneralPresenter;
import com.cheer.ba.utils.NetUtils;
import com.cheer.ba.view.base.IViewController;

import java.util.List;

import retrofit2.Call;
import retrofit2.Response;

/**
 * 首页数据处理
 */
public class FindOilPresenter extends GeneralPresenter {

    private Context mContext;
    private IViewController mView;
    private APIService mUserService;

    //记录当前分页下标
    private int mRecordPage = 0;
    private int mIndex = 0;

    //数据是否加载失败
    private boolean isDataError = false;

    @Override
    public void attachView(IViewController view) {
        this.mView = view;
        mContext = getContext(view);
        mUserService = Http.getUserService(mContext);
    }

    @Override
    public void detachView() {
    }

    /**
     * 找油
     *
     * @param isShowLoad   是否显示正在加载
     * @param loadMore
     * @param mOilArea     地区
     * @param mOilType     油品种类
     * @param mOilOriginal 油源
     * @param mOilRule     标准
     */
    public void findOils(boolean isShowLoad, final boolean loadMore, String mOilArea, String mOilType, String mOilOriginal, String mOilRule, String mSaleSort, String mPriceSort, String searchKey) {
        if (!NetUtils.isNetworkConnected(mContext)) {
            mView.onLoadingStatus(mView, true, CodeStatus.Gegeneral.ERROR_NET, CodeStatus.Gegeneral.ERROR_NET, "", loadMore);
            return;
        }
        if (!loadMore) {
            isDataError = false;
            mRecordPage = 0;
        } else {
            if (isDataError) {
                mRecordPage = mIndex;
            } else {
                mRecordPage += Constants.PAGESIZE;
            }
        }
        mIndex = mRecordPage;
        if (isShowLoad) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_LOADING, CodeStatus.Gegeneral.DATA_LOADING, "", loadMore);
        }
        Call<ResponseData<List<OilGoodsInfoModel>>> call = mUserService.getOilSaleList(1508, mOilArea, mOilOriginal, mOilType, mOilRule, mSaleSort, mPriceSort, "-1", searchKey, mRecordPage, Constants.PAGESIZE);
        CallManager.add(call);
        call.enqueue(new BaseCallback<ResponseData<List<OilGoodsInfoModel>>>() {
            @Override
            public void onResponse(Call<ResponseData<List<OilGoodsInfoModel>>> call, Response<ResponseData<List<OilGoodsInfoModel>>> response) {
                super.onResponse(call, response);
                if (response.isSuccessful()) {
                    ResponseData<List<OilGoodsInfoModel>> res = response.body();
                    if (res.getRet() == ReturnCode.SUCCESS) {
                        List<OilGoodsInfoModel> articles = res.getData();
                        if (null != articles) {
                            if (articles.isEmpty()) {
                                isDataError = true;
                            } else {
                                isDataError = false;
                            }
                            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, "", loadMore);
                            mView.updateViewWithLoadMore(articles, loadMore, ConstantTag.GET_OIL_LIST);
                            return;
                        }
                    }
                }
                isDataError = true;
                mView.onLoadingStatus(mView, true, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "", loadMore);
            }

            @Override
            public void onFailure(Call<ResponseData<List<OilGoodsInfoModel>>> call, Throwable t) {
                super.onFailure(call, t);
                isDataError = true;
                mView.onLoadingStatus(mView, true, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "", loadMore);
            }
        });
    }

    /**
     * 柴油批发详情
     */
    public void getOilDetails(String oilId, boolean isShowLoad) {
        if (!NetUtils.isNetworkConnected(mContext)) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_NET, CodeStatus.Gegeneral.ERROR_NET);
            return;
        }
        if (isShowLoad) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_LOADING, CodeStatus.Gegeneral.DATA_LOADING);
        }
        Call<ResponseData<OilDetailsModel>> call = mUserService.getOilDetails(1508, oilId);
        CallManager.add(call);
        call.enqueue(new BaseCallback<ResponseData<OilDetailsModel>>() {
            @Override
            public void onResponse(Call<ResponseData<OilDetailsModel>> call, Response<ResponseData<OilDetailsModel>> response) {
                super.onResponse(call, response);
                if (response.isSuccessful()) {
                    ResponseData<OilDetailsModel> res = response.body();
                    if (res.getRet() == ReturnCode.SUCCESS) {
                        OilDetailsModel model = res.getData();
                        if (null != model) {
                            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, CodeStatus.Gegeneral.DATA_SUCCESS_FULL);
                            mView.updateViewWithTag(model, ConstantTag.GET_OIL_DETAIL);
                            return;
                        }
                    }
                }
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA);
            }

            @Override
            public void onFailure(Call<ResponseData<OilDetailsModel>> call, Throwable t) {
                super.onFailure(call, t);
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA);
            }
        });
    }

    /**
     * 定金计算列表
     */
    public void getCalculateMoney() {
        if (!NetUtils.isNetworkConnected(mContext)) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_NET, CodeStatus.Gegeneral.ERROR_NET);
            return;
        }
        Call<ResponseData<List<CalculateMoneyModel>>> call = mUserService.getCalculateMoney(1508);
        CallManager.add(call);
        call.enqueue(new BaseCallback<ResponseData<List<CalculateMoneyModel>>>() {
            @Override
            public void onResponse(Call<ResponseData<List<CalculateMoneyModel>>> call, Response<ResponseData<List<CalculateMoneyModel>>> response) {
                super.onResponse(call, response);
                if (response.isSuccessful()) {
                    ResponseData<List<CalculateMoneyModel>> res = response.body();
                    if (res.getRet() == ReturnCode.SUCCESS) {
                        List<CalculateMoneyModel> mCalculates = res.getData();
                        if (null != mCalculates && !mCalculates.isEmpty()) {
                            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, CodeStatus.Gegeneral.DATA_SUCCESS_FULL);
                            mView.updateViewWithTag(mCalculates, ConstantTag.GET_CALCULATE_LIST);
                            return;
                        }
                    }
                }
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA);
            }

            @Override
            public void onFailure(Call<ResponseData<List<CalculateMoneyModel>>> call, Throwable t) {
                super.onFailure(call, t);
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA);
            }
        });
    }

    /**
     * 获得企业认证列表
     */
    public void getQyeAuthList(String name) {
        if (!NetUtils.isNetworkConnected(mContext)) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_NET, CodeStatus.Gegeneral.ERROR_NET);
            return;
        }
        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_LOADING, CodeStatus.Gegeneral.DATA_LOADING);
        Call<ResponseData<List<QyeAuthModel>>> call = mUserService.getOrgAuthList(1508, name);
        CallManager.add(call);
        call.enqueue(new BaseCallback<ResponseData<List<QyeAuthModel>>>() {
            @Override
            public void onResponse(Call<ResponseData<List<QyeAuthModel>>> call, Response<ResponseData<List<QyeAuthModel>>> response) {
                super.onResponse(call, response);
                if (response.isSuccessful()) {
                    ResponseData<List<QyeAuthModel>> res = response.body();
                    if (res.getRet() == ReturnCode.SUCCESS) {
                        List<QyeAuthModel> qyes = res.getData();
                        if (null != qyes) {
                            if (qyes.isEmpty()) {
                                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_SUCCESS_NULL, CodeStatus.Gegeneral.DATA_SUCCESS_NULL);
                            } else {
                                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, CodeStatus.Gegeneral.DATA_SUCCESS_FULL);
                                mView.updateViewWithTag(qyes, ConstantTag.GET_ORG_LIST);
                            }
                            return;
                        }
                    }
                }
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA);
            }

            @Override
            public void onFailure(Call<ResponseData<List<QyeAuthModel>>> call, Throwable t) {
                super.onFailure(call, t);
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA);
            }
        });
    }

    /**
     * 去认证
     */
    public void goToAuth(String orgId, String content) {
        if (!NetUtils.isNetworkConnected(mContext)) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_NET, CodeStatus.Gegeneral.ERROR_NET, mContext.getString(R.string.no_net_work));
            return;
        }
        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_LOADING, CodeStatus.Gegeneral.DATA_LOADING, mContext.getString(R.string.data_loading));
        Call<ResponseData> call = mUserService.goToAuth(1508, Session.getUserId(), orgId, content);
        CallManager.add(call);
        call.enqueue(new BaseCallback<ResponseData>() {
            @Override
            public void onResponse(Call<ResponseData> call, Response<ResponseData> response) {
                super.onResponse(call, response);
                if (response.isSuccessful()) {
                    ResponseData res = response.body();
                    if (res.getRet() == ReturnCode.SUCCESS) {
                        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, "");
                        mView.updateViewWithTag(res.getRet(), ConstantTag.GO_TO_AUTH_SUCCESS);
                        return;
                    } else {
                        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "");
                        HandleRetCode.handler(mContext, res, true, "提交失败，请重新认证");
                        return;
                    }
                }
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "提交失败，请重新认证");
            }

            @Override
            public void onFailure(Call<ResponseData> call, Throwable t) {
                super.onFailure(call, t);
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "提交失败，请重新认证");
            }
        });
    }

    /**
     * 我认证的企业
     */
    public void myAuthQye(String orgId) {
        if (!NetUtils.isNetworkConnected(mContext)) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_NET, CodeStatus.Gegeneral.ERROR_NET, mContext.getString(R.string.no_net_work));
            return;
        }
        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_LOADING, CodeStatus.Gegeneral.DATA_LOADING, mContext.getString(R.string.data_loading));
        Call<ResponseData<QyeAuthModel>> call = mUserService.myAuthQye(1508, Session.getUserId(), orgId);
        CallManager.add(call);
        call.enqueue(new BaseCallback<ResponseData<QyeAuthModel>>() {
            @Override
            public void onResponse(Call<ResponseData<QyeAuthModel>> call, Response<ResponseData<QyeAuthModel>> response) {
                super.onResponse(call, response);
                if (response.isSuccessful()) {
                    ResponseData<QyeAuthModel> res = response.body();
                    if (res.getRet() == ReturnCode.SUCCESS) {
                        QyeAuthModel model = res.getData();
                        if (null != model) {
                            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, "");
                            mView.updateViewWithTag(model, ConstantTag.MY_AUTH_QYE);
                            return;
                        }
                    }
                }
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "获取失败");
            }

            @Override
            public void onFailure(Call<ResponseData<QyeAuthModel>> call, Throwable t) {
                super.onFailure(call, t);
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "获取失败");
            }
        });
    }

    /**
     * 取消认证
     */
    public void cancelAuth(String orgId) {
        if (!NetUtils.isNetworkConnected(mContext)) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_NET, CodeStatus.Gegeneral.ERROR_NET, mContext.getString(R.string.no_net_work));
            return;
        }
        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_LOADING, CodeStatus.Gegeneral.DATA_LOADING, mContext.getString(R.string.data_loading));
        Call<ResponseData> call = mUserService.cancelAuthQye(1508, Session.getUserId(), orgId);
        CallManager.add(call);
        call.enqueue(new BaseCallback<ResponseData>() {
            @Override
            public void onResponse(Call<ResponseData> call, Response<ResponseData> response) {
                super.onResponse(call, response);
                if (response.isSuccessful()) {
                    ResponseData res = response.body();
                    if (res.getRet() == ReturnCode.SUCCESS) {
                        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, "");
                        mView.updateViewWithTag(res.getRet(), ConstantTag.CANCEL_AUTH_QYE);
                        return;
                    } else {
                        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "");
                        HandleRetCode.handler(mContext, res, true, "取消失败");
                        return;
                    }
                }
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "取消失败");
            }

            @Override
            public void onFailure(Call<ResponseData> call, Throwable t) {
                super.onFailure(call, t);
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "取消失败");
            }
        });
    }

    /**
     * 获得我的交易记录
     *
     * @param loadMore
     */
    public void getMyTrades(final boolean hadData, final boolean loadMore) {
        if (!NetUtils.isNetworkConnected(mContext)) {
            mView.onLoadingStatus(mView, hadData, CodeStatus.Gegeneral.ERROR_NET, CodeStatus.Gegeneral.ERROR_NET, "", loadMore);
            return;
        }
        if (!loadMore) {
            isDataError = false;
            mRecordPage = 0;
        } else {
            if (isDataError) {
                mRecordPage = mIndex;
            } else {
                mRecordPage += Constants.PAGESIZE;
            }
        }
        mIndex = mRecordPage;
        mView.onLoadingStatus(mView, hadData, CodeStatus.Gegeneral.DATA_LOADING, CodeStatus.Gegeneral.DATA_LOADING, "", loadMore);
        Call<ResponseData<List<MyTradeRecordModel>>> call = null;
//                mUserService.getMyTrades(1508, Session.getUserId(), "0", mRecordPage, Constants.PAGESIZE);
        CallManager.add(call);
        call.enqueue(new BaseCallback<ResponseData<List<MyTradeRecordModel>>>() {
            @Override
            public void onResponse(Call<ResponseData<List<MyTradeRecordModel>>> call, Response<ResponseData<List<MyTradeRecordModel>>> response) {
                super.onResponse(call, response);
                if (response.isSuccessful()) {
                    ResponseData<List<MyTradeRecordModel>> res = response.body();
                    if (res.getRet() == ReturnCode.SUCCESS) {
                        List<MyTradeRecordModel> articles = res.getData();
                        if (null != articles) {
                            if (articles.isEmpty()) {
                                isDataError = true;
                                mView.onLoadingStatus(mView, hadData, CodeStatus.Gegeneral.DATA_SUCCESS_NULL, CodeStatus.Gegeneral.DATA_SUCCESS_NULL, "", loadMore);
                            } else {
                                isDataError = false;
                                mView.onLoadingStatus(mView, hadData, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, "", loadMore);
                            }
                            mView.updateViewWithLoadMore(articles, loadMore, ConstantTag.GET_MY_TRADE);
                            return;
                        }
                    }
                }
                isDataError = true;
                mView.onLoadingStatus(mView, hadData, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "", loadMore);
            }

            @Override
            public void onFailure(Call<ResponseData<List<MyTradeRecordModel>>> call, Throwable t) {
                super.onFailure(call, t);
                isDataError = true;
                mView.onLoadingStatus(mView, hadData, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "", loadMore);
            }
        });
    }

    /**
     * 油卡变更记录
     *
     * @param loadMore
     */
    public void getMyDevice(final boolean hadData, final boolean loadMore, String cardId) {
        if (!NetUtils.isNetworkConnected(mContext)) {
            mView.onLoadingStatus(mView, hadData, CodeStatus.Gegeneral.ERROR_NET, CodeStatus.Gegeneral.ERROR_NET, "", loadMore);
            return;
        }
        if (!loadMore) {
            isDataError = false;
            mRecordPage = 0;
        } else {
            if (isDataError) {
                mRecordPage = mIndex;
            } else {
                mRecordPage += Constants.PAGESIZE;
            }
        }
        mIndex = mRecordPage;
        mView.onLoadingStatus(mView, hadData, CodeStatus.Gegeneral.DATA_LOADING, CodeStatus.Gegeneral.DATA_LOADING, "", loadMore);
        Call<ResponseData<List<MyTradeRecordModel>>> call = null;
//                mUserService.changeOilCard(1508, cardId, "0", "", mRecordPage, Constants.PAGESIZE);
        CallManager.add(call);
        call.enqueue(new BaseCallback<ResponseData<List<MyTradeRecordModel>>>() {
            @Override
            public void onResponse(Call<ResponseData<List<MyTradeRecordModel>>> call, Response<ResponseData<List<MyTradeRecordModel>>> response) {
                super.onResponse(call, response);
                if (response.isSuccessful()) {
                    ResponseData<List<MyTradeRecordModel>> res = response.body();
                    if (res.getRet() == ReturnCode.SUCCESS) {
                        List<MyTradeRecordModel> articles = res.getData();
                        if (null != articles) {
                            if (articles.isEmpty()) {
                                isDataError = true;
                                mView.onLoadingStatus(mView, hadData, CodeStatus.Gegeneral.DATA_SUCCESS_NULL, CodeStatus.Gegeneral.DATA_SUCCESS_NULL, "", loadMore);
                            } else {
                                isDataError = false;
                                mView.onLoadingStatus(mView, hadData, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, "", loadMore);
                            }
                            mView.updateViewWithLoadMore(articles, loadMore, ConstantTag.GET_MY_TRADE);
                            return;
                        }
                    }
                }
                isDataError = true;
                mView.onLoadingStatus(mView, hadData, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "", loadMore);
            }

            @Override
            public void onFailure(Call<ResponseData<List<MyTradeRecordModel>>> call, Throwable t) {
                super.onFailure(call, t);
                isDataError = true;
                mView.onLoadingStatus(mView, hadData, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "", loadMore);
            }
        });
    }


    /**
     * @return
     */
    public Object getData(String tag) {
        return null;
    }

    @Override
    public Object getCacheData() {
        return null;
    }
}
